// 引入公用的自定义函数
var $ = require('../../../utils/js.js');
var img_gy_src = $.img_gy_src;

Page({
  // 页面的初始数据
  data: {
    // 图片路径的公用部分
    img_gy_src: img_gy_src,

    // 获取到的用户信息
    user_info: "",
    // 拥有店铺的状态  0->未拥有 1->拥有 2->审核中
    has_shop: "0",
    // 商家的星级
    star: 0,

    // 获取到的我的店铺数据
    my_shop_data: "",

    // 管理与设置中的数据
    gl_sz_data: [{
      src: img_gy_src + "/my/my_shop/fbdt.png",
      url: "/pages/index/fabu/fabu",
      title: "发布动态"
    }, {
      src: img_gy_src + "/my/my_shop/ckdp.png",
      url: "",
      title: "查看店铺"
    }, {
      src: img_gy_src + "/my/my_shop/wdmp.png",
      url: "/pages/my/my_name_card/my_name_card?back=1",
      title: "我的名片"
    }, {
      src: img_gy_src + "/my/my_shop/bjdp.png",
      url: "",
      title: "编辑店铺"
    }, {
      src: img_gy_src + "/my/my_shop/khgl.png",
      url: "",
      title: "客户管理",
      ti_shi: "暂未开通",
    }],

    // 营销与推广中的数据
    yx_tg_data: [{
      src: img_gy_src + "/my/my_shop/lbtg.png",
      url: "",
      title: "裂变推广",
      ti_shi: "暂未开通",
    }, {
      src: img_gy_src + "/my/my_shop/mpgc.png",
      url: "/pages/index/name_card_list/name_card_list",
      title: "名片广场"
    }, {
      src: img_gy_src + "/my/my_shop/yjts.png",
      url: "",
      title: "一键推送",
      ti_shi: "暂未开通",
    }, {
      src: img_gy_src + "/my/my_shop/yxhd.png",
      url: "",
      title: "营销活动",
      ti_shi: "暂未开通",
    }, {
      src: img_gy_src + "/my/my_shop/dlxcx.png",
      url: "/pages/my/jia_meng_dian_pu/jia_meng_dian_pu",
      title: "独立小程序",
      bq: "高级",
    }],

    // 是否刷新页面
    shua_xin: false,
    back: "",
  },
  // 生命周期函数--监听页面加载
  onLoad: function(options) {
    this.setData({
      back: options.back ? options.back : ""
    })
    if (getApp().data.hide) {
      var gl_sz_data = this.data.gl_sz_data;
      gl_sz_data.splice(0, 1);
      this.setData({
        gl_sz_data: gl_sz_data
      })
    }
    // 获取用户信息
    this.get_user_data();
  },
  // 生命周期函数--监听页面显示
  onShow: function() {
    if (this.data.shua_xin) {
      // 获取用户信息
      this.get_user_data();
    } else {
      this.setData({
        shua_xin: true
      })
    }
  },
  // 自定义函数   开始
  // 获取用户信息
  get_user_data() {
    $.get_user_data((res) => {
      console.log("用户信息", res);
      var has_shop = res.has_shop;
      this.setData({
        user_info: res,
        has_shop: has_shop
      })
      if (has_shop == 1) { //如果有店铺
        this.shop_info(res.shopid);
        //获取店铺id
        var url = "/pages/shang_quan/dian_pu_xiang_qing/dian_pu_xiang_qing?id=" + res.shopid;
        if (getApp().data.hide) {
          this.setData({
            "gl_sz_data[0].url": url,
            "gl_sz_data[2].url": "/pages/my/edit_my_shop/edit_my_shop?id=" + res.shopid
          })
        } else {
          this.setData({
            "gl_sz_data[1].url": url,
            "gl_sz_data[3].url": "/pages/my/edit_my_shop/edit_my_shop?id=" + res.shopid
          })
        }
      } else if (has_shop == 2) { // 店铺审核中
        if (getApp().data.hide) {
          this.setData({
            "gl_sz_data[0].ti_shi": "店铺审核中,通过后即可查看哦！",
            "gl_sz_data[2].title": "审核中...",
            "gl_sz_data[2].ti_shi": "店铺审核中...",
          })
        } else {
          this.setData({
            "gl_sz_data[1].ti_shi": "店铺审核中,通过后即可查看哦！",
            "gl_sz_data[3].title": "审核中...",
            "gl_sz_data[3].ti_shi": "店铺审核中...",
          })
        }
      } else { // 没有店铺
        if (getApp().data.hide) {
          this.setData({
            "gl_sz_data[0].ti_shi": "您还没有店铺哦，快去入驻吧！",
            "gl_sz_data[2].title": "免费入驻",
            "gl_sz_data[2].url": "/pages/my/get_my_shop/get_my_shop",
          })
        } else {
          this.setData({
            "gl_sz_data[1].ti_shi": "您还没有店铺哦，快去入驻吧！",
            "gl_sz_data[3].title": "免费入驻",
            "gl_sz_data[3].url": "/pages/my/get_my_shop/get_my_shop",
          })
        }
      }
    })
  },
  // 获取店铺信息
  shop_info(shop_id) {
    var that = this;
    $.ajax({
      url: "/shops/info",
      method: "GET",
      data: {
        shop_id: shop_id
      },
      success: function(res) {
        var data = res.data.data;
        console.log("店铺信息", data);
        //将分数四舍五入
        var score = data.score;
        data.score = score.toFixed(1);
        that.setData({
          my_shop_data: data
        });
        
        that.setData({
          xing_ji: [score - 0, score - 1, score - 2, score - 3, score - 4]
        })
      }
    });
  },

  //点击立即注册
  sign_shop() {
    $.open("/pages/my/get_my_shop/get_my_shop");
  },
  // 管理与设置的点击事件
  gl_sz_fun(e) {
    var index = $.data(e, 'index');
    var gl_sz_data = this.data.gl_sz_data;
    var url = gl_sz_data[index].url;
    if (url) {
      if (url == "/pages/my/my_name_card/my_name_card?back=1") {
        if (this.data.back) {
          $.back(this.data.back);
        } else {
          $.open(url);
        }
      } else {
        $.open(url);
      }
    } else {
      $.ti_shi({
        title: gl_sz_data[index].ti_shi
      });
    }
  },
  // 营销与推广的点击事件
  yx_tg_fun(e) {
    var index = $.data(e, 'index');
    var yx_tg_data = this.data.yx_tg_data;
    var url = yx_tg_data[index].url;
    if (url) {
      $.open(url);
    } else {
      $.ti_shi({
        title: yx_tg_data[index].ti_shi
      });
    }
  },
  // 点击分享好友
  fen_xiang_fun(e) {
    var title = $.data(e, "title");
    $.ti_shi({
      title: title,
    })
  },
  //点击查看店铺小程序码
  see_code() {
    var qr_code = this.data.my_shop_data.qr_code_xcx;
    $.yu_lan(qr_code, [qr_code]);
  },
  // 自定义函数   结束
  // 生命周期函数--监听页面隐藏
  onHide: function() {

  },
  // 生命周期函数--监听页面卸载
  onUnload: function() {

  },
  // 页面相关事件处理函数--监听用户下拉动作
  onPullDownRefresh: function() {

  },
  // 页面上拉触底事件的处理函数
  onReachBottom: function() {

  },
  // 用户点击右上角分享
  onShareAppMessage: function() {
    var that = this;
    var path = "/pages/shang_quan/dian_pu_xiang_qing/dian_pu_xiang_qing?id=" + that.data.my_shop_data.id;
    return {
      title: that.data.my_shop_data.shop_name,
      path: path,
    }
  }
})